<?php
/**
 * 会员(前台用户)管理控制器
 */

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;

class MemberController extends AdminBaseController
{

    public function initialize()
    {
        $adminSettings = cmf_get_option('admin_settings');
        if (empty($adminSettings['admin_password']) || $this->request->path() == $adminSettings['admin_password']) {
            $adminId = cmf_get_current_admin_id();
            if (empty($adminId)) {
                session("__LOGIN_BY_CMF_ADMIN_PW__", 1);//设置后台登录加密码
            }
        }

        parent::initialize();
    }

    /**
     * 会员首页
     */
    public function index()
    {
        $where = '1 = 1';
        //搜索条件
        $level = $this->request->param('level',-1,'intval');
        $account_status = $this->request->param('account_status',-1,'intval');
        $user_name = $this->request->param('user_name','');
        $mobile = $this->request->param('mobile','');
        $searchInfo = array('level'=>$level,'account_status'=>$account_status);
        $users = Db::name('shop_user')
            ->where($where)
            ->where(function (Query $query) use ($level,$account_status,$user_name,$mobile){
                if($level >= 0 ){
                    $query->where('level',$level);
                }
                if($account_status >= 0){
                    $query->where('account_status',$account_status);
                }
                if($user_name){
                    $query->where('user_name','like',"%$user_name%");
                }
                if($mobile){
                    $query->where('mobile','like',"%$mobile%");
                }
            })
            ->order('user_id desc')
            ->paginate(10);
        $users->appends(['level'=>$level,'account_status'=>$account_status,'user_name'=>$user_name,'mobile'=>$mobile]);
        $page = $users->render();
        $this->assign('users',$users);
        $this->assign('page',$page);
        $this->assign('searchInfo',$searchInfo);
        return $this->fetch();
    }

    /**
     * 冻结|解冻账号
     * status: 账号状态，0正常，1冻结
     */
    function freeze(){
        $user_id = $this->request->param('id',0,'intval');
        $account_status = $this->request->param('status',0,'intval');
        if(!$user_id){
            $this->error('数据传入失败！');
        }
        $info = array('user_id'=>$user_id,'account_status'=>$account_status);
        $rs = Db::name('shop_user')->update($info);
        if($rs){
            $this->success('修改成功！');
        }else{
            $this->error('修改失败！');
        }
    }

    /**
     * 用户详情
     */
    function detail(){
        $user_id = $this->request->param('id',0,'intval');
        if(!$user_id){
            $this->error('数据传入失败！');
        }
        $user = Db::name('shop_user')->where('user_id',$user_id)->find();
        $this->assign('user',$user);
        //银行卡
        $cardList = Db::name('credit')->where('user_id',$user_id)->select();
        $this->assign('cardList',$cardList);
        return $this->fetch();
    }
    /**
     * 充值页面
     */
    public function recharge()
    {
        $user_id = input('user_id/d');
        if (empty($user_id)) {
            $this->error('操作失败');
        }
        $info = Db::name('shop_user')->where('user_id',$user_id)->find();
        if (empty($info)) {
           $this->error('查无此人');
        }
        $this->assign('info',$info);
        return $this->fetch();
    }
    /**
     * 充值
     */
    public function addMoney()
    {
        $user_id = input('user_id/d');
        $money = input('money');
        if (empty($user_id) || empty($money)) {
            $this->error('参数出错');
        }
        if (!preg_match("/^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/", $money)) {
            $this->error('请输入正确金额');
        }
        if (intval($money)>9999999999) {
            $this->error('金额过大，系统已经崩溃');
        }
        $admin_id = session('ADMIN_ID');
        $time = time();
        $data = [];
        $data['order_sn'] = cmf_get_order_sn();
        $data['money'] = $money;
        $data['create_time'] = $time;
        $data['status'] = 2;
        $data['pay_time'] = $time;
        $data['user_id'] = $user_id;
        Db::startTrans();
        try {

            $id = Db::name('recharge')->insertGetId($data);

            Db::name('balance_record')->insert(['money'=>$money,'scene' => 2,'sign'=>1,'create_time'=>date('Y-m-d H:i:s',$time),'user_id'=>$user_id,'remarks'=>'操作的后台管理员'.$admin_id,'data_id'=>$id]);
            Db::name('shop_user')->where('user_id',$user_id)->setInc('balance',$money);
            Db::commit(); 
            $this->success('充值成功');
        } catch (Exception $e) {
            Db::rollback(); 
            $this->error('充值失败');
        }
    }
}
